<?php 

class LsWebApplication
extends CWebApplication
{
	
	protected $_last_controller;
	public $holdController = true;
	
	/**
	 * @param string $language the language that the source messages are written in.
	 */
	public function setLanguage($language){
		$language = LsLocale::getCanonicalID($language);
		parent::setLanguage($language);
	}
	
	
	/**
	 * Raised right AFTER the application processes the request.
	 * @param CEvent $event the event parameter
	 */
	public function onEndRequest($event){
		parent::onEndRequest($event);
		if(Yii::$autoload_errors){
			Yii::trace("Autoload errors:\r\n\t".join("\r\n\t", Yii::$autoload_errors));
		}
	}
	
	public function createDefaultController($set = true){
		if(($ca = $this->createController('')) !== null){
			list($controller,) = $ca;
			if($set){
				$this->setController($controller);
			}
			$controller->init();
			return $controller;
		}
	}
	
	public function getController(){
		if(!($controller = parent::getController())){
			$controller = $this->createDefaultController();
		}
		if($this->holdController
		&& $controller->getId()=='_lsyii'
		&& ($lastController = $this->getLastController())
		){
			return $lastController;
		}
		return $controller;
	}
	
	/**
	 * Creates the controller and performs the specified action.
	 * @param string $route the route of the current request. See {@link createController} for more details.
	 * @throws CHttpException if the controller could not be created.
	 */
	public function runController($route)
	{
		if(($ca=$this->createController($route))!==null)
		{
			list($controller,$actionID)=$ca;
			$oldContoller=$this->getController();
			$this->setController($controller);
			$controller->init();
			$controller->run($actionID);
			if($this->holdController) $this->_last_controller = $controller;
			$this->setController($oldContoller);
		}
		else
			throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
				array('{route}'=>$route===''?$this->defaultController:$route)));
	}
	
	/**
	 * @return LsyiiController
	 */
	public function getLastController(){
		return $this->_last_controller;
	}
	
}

?>